from django.db import models
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from datetime import datetime, timezone


class User(AbstractUser):
    username = models.CharField(max_length=32, unique=True, db_index=True, validators=[AbstractUser.username_validator])
    email = models.EmailField(null=False, blank=True, db_index=True)
    metadata: dict = models.JSONField(default=dict, blank=True)

    first_name = None
    last_name = None

    get_full_name = get_short_name = get_username = lambda self: self.username

    Meta = type("Meta", (), {"db_table": "auth_user"})

    def get_avatar(self):
        return ProfilePhoto.objects.get(username=self.username).pic.url

    @property
    def avatar(self):
        return self.get_avatar()


def now():
    return datetime.utcnow().replace(tzinfo=timezone.utc)


class Project(models.Model):
    creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    description = models.TextField(max_length=10240)
    release_datetime = models.DateTimeField(default=now)
    metadata = models.JSONField(default=dict, blank=True)


class ProfilePhoto(models.Model):
    username = models.CharField(max_length=32, unique=True, db_index=True, validators=[AbstractUser.username_validator])
    pic = models.ImageField(upload_to="pic/", verbose_name=u"图片地址")
